package com.zzstm.educms.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzstm.commonUtils.R;
import com.zzstm.educms.entity.CrmBanner;
import com.zzstm.educms.service.CrmBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-11-09
 */
@RestController
@RequestMapping("/educms/bannerAdmin")
@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService crmBannerService;

    //分页查询所有banner
    @GetMapping("/pageBanner/{pageNum}/{pageSize}")
    public R pageBanner(@PathVariable long pageNum,@PathVariable long pageSize){
        Page<CrmBanner> page = new Page<>(pageNum,pageSize);
        IPage<CrmBanner> result = crmBannerService.page(page, null);
        List<CrmBanner> records = result.getRecords();
        long total = result.getTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);
        return R.ok().data("items",map);
    }

    //根据id获取banner
    @GetMapping("/getBanenr/{id}")
    public R getBanner(@PathVariable String id){
        CrmBanner banner = crmBannerService.getById(id);
        return R.ok().data("banner",banner);
    }

    //添加banner
    @PostMapping("/addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.save(crmBanner);
        return R.ok();
    }

    //删除banner
    @DeleteMapping("/delBanner/{id}")
    public R delBanner(@PathVariable String id){
        crmBannerService.removeById(id);
        return R.ok();
    }

    //修改banner
    @PostMapping("/updateBanner")
    public R updateBanner(@RequestBody CrmBanner crmBanner){
        crmBannerService.updateById(crmBanner);
        return R.ok();
    }

}

